home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Lattice C v5.02 d4.adf / examples / lines.c < prev    next >
C/C++ Source or Header  |  1988-11-07  |  4KB  |  177 lines

  1. /* This is a line drawing demo for the Commodore/Amiga  */
  2. /* Written by    John Riley, Lattice, Inc.        */
  3. /*                             */
  4. /* there is a single compile time option:        */
  5. /*                            */
  6. /* compile with -dNOERASE  - leave trail of lines    */
  7. /*                            */
  8. /* if the option is not used then old lines will be    */
  9. /* erased as the lines are drawn across the screen    */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/nodes.h>
  13. #include <exec/lists.h>
  14. #include <intuition/intuition.h>
  15. #include <graphics/text.h>
  16. #include <proto/exec.h>
  17. #include <proto/graphics.h>
  18. #include <proto/intuition.h>
  19.  
  20. USHORT wakeup;    /* Wake me up for event */
  21. USHORT class;    /* Intu event class */
  22. USHORT code;    /* Intu event code */
  23.  
  24. struct Window *w;
  25. struct RastPort *rp,*cdrp;
  26. struct ViewPort *vp;
  27. struct IntuiMessage *message;
  28. int event(void);
  29. long rand(void);
  30.  
  31. /************************ Window Defines ***********************************/
  32.  
  33. struct NewWindow nw = {
  34.         100,100,            /* Starting corner */
  35.         300,100,        /* Width, height */
  36.         2,1,            /* detail, block pens */
  37.     CLOSEWINDOW | NEWSIZE,        /* IDCMP flags */
  38.     WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO | WINDOWSIZING,
  39.                     /* Window flags */
  40.         NULL,            /* Pointer to first gadget */
  41.         NULL,            /* Pointer to checkmark */
  42.         "Nervous Lines",    /* title */
  43.         NULL,            /* screen pointer */
  44.         NULL,            /* bitmap pointer */
  45.         50,50,640,400,        /* window not sized */
  46.         WBENCHSCREEN        /* type of screen */
  47.         };
  48.  
  49. int x[2],y[2],xd[2],yd[2],co,ox[2][16],oy[2][16],xx[128],xlim,ylim;
  50.  
  51.  
  52. /**
  53. *
  54. * main program - Since no command line data is used, and no file I/O
  55. * is performed, we do not need _main from the library. We eliminate
  56. * it by calling our main function '_main'.
  57. *
  58. **/
  59. void _main()
  60. {
  61.     register short i;
  62.     register int k,co;
  63.     register long j;
  64.  
  65. /************************ Set-Up routines **********************************/
  66.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  67.     if(GfxBase == NULL) return;
  68.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  69.     if(IntuitionBase == NULL) 
  70.         {
  71.         CloseLibrary((struct Library *)GfxBase);
  72.         return;
  73.         }
  74.     w = OpenWindow(&nw);
  75.     rp = w->RPort;            /* Get the raster port pointer */
  76.     vp = &w->WScreen->ViewPort;    /* Get the view port pointer */
  77.     SetAPen(rp,3);            /* Set foreground pen to white */
  78.     SetDrMd(rp,JAM1);        /* Draw with foreground pen */
  79.  
  80.     xlim = w->Width;
  81.     ylim = w->Height;
  82.     for(i=0;i<2;i++)
  83.     {
  84.         x[i] = rand() % xlim + 1;
  85.         y[i] = rand() % ylim + 1;
  86.         xd[i] = rand() % 10 + 1;
  87.         yd[i] = rand() % 10 + 1;
  88.     }
  89.     co = 1;
  90.     j = 0;
  91.     do {
  92. #ifndef NOERASE
  93.         SetAPen(rp,0);
  94.         Move(rp,ox[0][j & 15],oy[0][j & 15]);
  95.         Draw(rp,ox[1][j & 15 ],oy[1][j & 15]);
  96. #endif
  97.         SetAPen(rp,co);
  98.         Move(rp,x[0],y[0]);
  99.         Draw(rp,x[1],y[1]);
  100.         if((rand() & 127) < 2)
  101.         {
  102.             ++co;
  103.             if(co > 3)    co = 1;
  104.             SetAPen(rp,co);
  105.         }
  106.         for(i=0;i<2;i++)
  107.         {
  108.             ox[i][(j+10) & 15] = x[i];
  109.             oy[i][(j+10) & 15] = y[i];
  110.             x[i] += xd[i];
  111.             y[i] += yd[i];
  112.             if(x[i] < 2)
  113.             {
  114.                 x[i] = 2;
  115.                 xd[i] = -xd[i];
  116.             }
  117.             else if(x[i] > xlim)
  118.             {
  119.                 x[i] = xlim;
  120.                 xd[i] = -xd[i];
  121.             }
  122.             if(y[i] < 2)
  123.             {
  124.                 y[i] = 2;
  125.                 yd[i] = -yd[i];
  126.             }
  127.             else if(y[i] > ylim)
  128.             {
  129.                 y[i] = ylim;
  130.                 yd[i] = -yd[i];
  131.             }
  132.             if(((rand() >> 5) & 127) < 2)
  133.             {
  134.                 if(xd[i] < 1)     k = 1;
  135.                 xd[i] = (rand() >> 5) & 7;
  136.                 if(k == 1)    xd[i] = -xd[i];
  137.                 k = 0;
  138.             } 
  139.             if(((rand() >> 5) & 255) < 50)
  140.             {
  141.                 if(yd[i] < 1)    k = 1;
  142.                 yd[i] = (rand() >> 5) & 7;
  143.                 if(k == 1)    yd[i] = -yd[i];
  144.                 k = 0;
  145.             }
  146.         }
  147.         ++j;
  148.         if(w->UserPort->mp_SigBit)
  149.         {
  150.             message = (struct IntuiMessage *)GetMsg(w->UserPort);
  151.             if(message != NULL)
  152.             {
  153.                 class = message->Class;
  154.                 code = message->Code;
  155.                 ReplyMsg((struct Message *)message);
  156.             }
  157.         }
  158.     } while(event());
  159.     CloseWindow(w);
  160.     CloseLibrary((struct Library *)GfxBase);
  161.     CloseLibrary((struct Library *)IntuitionBase);
  162. }
  163.  
  164. int event()
  165. {
  166.     switch(class)
  167.     {
  168.         case CLOSEWINDOW:
  169.             return(0);
  170.         case NEWSIZE:
  171.             xlim = w->Width;
  172.             ylim = w->Height;
  173.             return(1);
  174.     }
  175.     return(1);
  176. }
  177.